package switchviz.network;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import switchviz.core.data.OIDDataPair;
import switchviz.network.netswitch.Switch;
import switchviz.network.netswitch.SwitchDataModel;


public class NetworkFacade
{
	private static final String IF_NUMBER = "1.3.6.1.2.1.2.1.0";
	private static final String IF_TYPE = "1.3.6.1.2.1.2.2.1.3";
	private static final String SYSDESCR = "1.3.6.1.2.1.1.1.0";
	
	private static final int ETHERNET_CSMACD = 6;
	private static final int ISO88023_CSMACD = 7;
	private static final int ISO88024_TOKENBUS = 8;
	private static final int ISO88025_TOKENRING = 9;
	private static final int ATM = 37;
	private static final int SONET = 39;

	public static String getSwitchType(SwitchDataModel dataModel) throws NetworkException
	{
		return requestData(dataModel, SYSDESCR).toString();
	}
	
	public static List<String> getNumberOfPorts(SwitchDataModel dataModel) throws NetworkException, IllegalArgumentException, IOException
	{
		List<String> oidlist = new ArrayList<String>();
		Object retnum = requestData(dataModel, IF_NUMBER);
		if(retnum == null)
			return null;
		
		int portnum = Integer.valueOf(retnum.toString());
		OIDDataPair[] objarray = requestBulkData(dataModel, IF_TYPE, portnum);
		for(int i = 0; i < objarray.length; ++i)
		{
			if(checkPortType((Integer)objarray[i].getObject()))
				oidlist.add(objarray[i].getPort());
		}
		
		return oidlist;
	}
	
	private static boolean checkPortType(int type)
	{
		switch(type)
		{
		case ETHERNET_CSMACD:
			return true;
		case ISO88023_CSMACD:
			return true;
		case ISO88024_TOKENBUS:
			return true;
		case ISO88025_TOKENRING:
			return true;
		case ATM:
			return true;
		case SONET:
			return true;
		default:
			return false;
		}
	}
	
	public static Object[] requestData(SwitchDataModel switchDataModel, String[] oid) throws NetworkException
	{
		return Switch.requestData(switchDataModel, oid);
	}
	
	public static Object requestData(SwitchDataModel switchDataModel, String oid) throws NetworkException
	{
		return Switch.requestData(switchDataModel, new String[]{oid})[0];
	}
	
	public static Object[] requestNextData(SwitchDataModel switchDataModel, String oid) throws NetworkException
	{
		return Switch.requestNextData(switchDataModel, new String[]{oid});
	}
	
	public static OIDDataPair[] requestBulkData(SwitchDataModel switchDataModel, String oid, int limit) throws IllegalArgumentException, IOException
	{
		return Switch.requestBulkData(switchDataModel, oid, limit);
	}
}
